package com.caption.mq.core;

import com.caption.mq.entity.MqSubscribeDO;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * TODO 可以手动执行，也可以定时任务执行
 */
public class MessageDistributeCenter {
    private static Map<String, String> updateMqSubscribeMap = new ConcurrentHashMap<>(256);
    //TODO注入
    private MqConsumerManger mqConsumerManger;

    /**
     * TODO 查询数据库
     *
     * @return
     */
    public List<MqSubscribeDO> getAllMessageSubscribe() {
        return null;
    }

    public void refresh(List<MqSubscribeDO> subscribeList) {
        for (MqSubscribeDO subscribe : subscribeList) {
            String consumerId = subscribe.getConsumerId();
            String updateTime = updateMqSubscribeMap.get(consumerId);
            String nowDate = "2021-12-31 00:00:00";
            if (updateTime != null) {
                mqConsumerManger.stopConsumer(subscribe);
                AbstractMqConsumer consumer = mqConsumerManger.createConsumer(subscribe);
                mqConsumerManger.startConsumer(consumer);
                updateMqSubscribeMap.put(consumerId, nowDate);
            }

        }
    }

}
